import Vue from 'vue'
import Router from 'vue-router'
import login from '../views/Login.vue'
import main from '../views/Main.vue'
import users from '../views/User.vue';
import roles from '../views/Roles.vue'
import rights from '../views/Rights.vue'
import categories from '../views/Categories.vue'
import params from '../views/Params.vue'
import goods from '../views/Goods.vue'
import addGoods from '../views/addGoods.vue'
import orders from '../views/Orders.vue'
import reports from '../views/Reports.vue'
Vue.use(Router)

let router = new Router({
    routes: [{
        path: '/',
        redirect: login
    }, {
        path: '/login',
        component: login
    }, {
        path: '/main',
        component: main,
        children: [
            { path: '/users', component: users },
            { path: '/roles', component: roles },
            { path: '/rights', component: rights },
            { path: '/categories', component: categories },
            { path: '/params', component: params },
            { path: '/goods', component: goods },
            { path: '/goods/add', component: addGoods },
            { path: '/orders', component: orders },
            { path: '/reports', component: reports }
        ]
    }],
})
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
        return originalPush.call(this, location).catch(err => err)
    }
    // 路由守卫
    // to:去往的地方  form：来的地方  next 
router.beforeEach((to, form, next) => {
    if (to.path === '/login') {
        next()
    } else {
        let curToken = sessionStorage.getItem('token')
        if (curToken) {
            next()
        } else {
            next('/login')
        }
    }
})
export default router